<template>
  <div>
    <div class="tool-bar">
      <el-row :gutter="20" class="h_top">
        <el-form ref="form" inline label-width="82px" label-position="left" class="nb-form">
          <el-col :span="5">
            <el-form-item label="客户单位：">
              <el-input v-model="query.agent" />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="项目名称：">
              <v-select-s v-model="query.project_id" action="projectList" />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="状态：">
              <el-select v-model="query.status" placeholder="请选择">
                <el-option v-for="item in invoiceStatus" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="创建时间：">
              <el-date-picker v-model="query.create_time" type="daterange" placeholder="" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="合同编号：">
              <el-input v-model="query.contract_code" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item>
              <el-button type="primary" @click="handleSearch">搜索</el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <el-row :gutter="20" class="bar-btn" style="margin-top: 30px">
        <el-button type="primary" icon="el-icon-circle-plus-outline" @click="handleAdd">新增</el-button>
        <el-button type="primary" icon="el-icon-upload2" class="button-hidden">导出</el-button>
        <el-button type="primary" icon="el-icon-delete" @click="delBtn">删除</el-button>
        <el-button type="primary" icon="el-icon-refresh-right" @click="refresh">刷新</el-button>
      </el-row>
    </div>
    <div class="nb-content">
      <el-table ref="multipleTable" v-loading="loading" border :data="data.dataList" tooltip-effect="dark" class="nb-table" @selection-change="handleSelectionChange">
        <el-table-column label="序号" type="index" align="center" width="55" />
        <el-table-column type="selection" align="center" width="55" />
        <el-table-column prop="agent" label="客户单位" align="center" show-overflow-tooltip width="200" />
        <el-table-column prop="project.name" label="项目名称" align="center" show-overflow-tooltip width="180" />
        <el-table-column prop="status" label="状态" align="center">
          <template slot-scope="{row}">
            <div v-if="row.status === 1" style="color: gray">申请</div>
            <div v-if="row.status === 2" style="color: blue">审核</div>
            <div v-if="row.status === 3" style="color: orange">审批</div>
            <div v-if="row.status === 4" style="color: green">完成</div>
          </template>
        </el-table-column>
        <el-table-column prop="contract_code" label="合同编号" align="center" width="180" />
        <el-table-column prop="invoice_times" label="发票次数" align="center" width="120" />
        <el-table-column prop="invoice_sum" label="开票金额" align="center" width="100" />
        <el-table-column v-if="userId === 1" prop="create_time" label="创建时间" align="center" width="180" />
        <el-table-column v-if="userId === 1" prop="cr_admin.name" label="创建人" align="center" />
        <el-table-column label="操作" fixed="right" width="180" align="center">
          <template scope="{row}">
            <el-button type="primary" @click="goDetail(row.id)">编辑</el-button>
            <el-button type="danger" @click="handleDel(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination class="page" background :page-size="query.limit" layout="total,prev,pager,next,jumper" :total="data.total" :current-page.sync="query.page" @current-change="index" @size-change="index" />
    </div>
  </div>
</template>

<script>
import API from '@/api/invoice/invoice'
import VSelectS from '@/components/select-search'
import { mapGetters } from 'vuex'
import { invoiceStatus } from '@/utils/options'
export default {
  inject: ['reload', 'goUrl'],
  components: { VSelectS },
  data() {
    return {
      data: {
        dataList: [],
        total: 0
      },
      query: {
        limit: 15,
        page: 1
      },
      loading: true,
      multipleSelection: [],
      invoiceStatus
    }
  },
  computed: {
    ...mapGetters(['userId'])
  },
  created() {
    this.index()
  },
  methods: {
    index() {
      this.loading = true
      API.invoiceList(this.query).then(res => {
        this.data.dataList = res.data.data
        this.data.total = res.data.total
      }).finally(() => {
        this.loading = false
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    goDetail(id) {
      const url = `/invoice/edit/${id}`
      this.goUrl(url)
    },
    handleDel(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        API.delete({ id }).then(res => {
          if (res.data.error_code !== 1) {
            this.$message.error(res.data.message)
          } else {
            this.$message.success('删除成功')
            this.reload()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    delBtn() {
      if (this.multipleSelection && this.multipleSelection.length > 0) {
        const idArray = []
        this.multipleSelection.forEach(item => {
          idArray.push(item.id)
        })
        const id = idArray.join(',')
        this.handleDel(id)
      } else {
        this.$message('请先选择，然后再删除')
      }
    },
    handleSearch() {
      this.query.page = 1
      this.index()
    },
    refresh() {
      this.reload()
    },
    handleAdd() {
      this.goUrl('/invoice/add')
    }
  }
}
</script>

<style>

.nb-form .el-form-item__content{
  width: calc(100% - 82px);
}
</style>
